
package com.debao.common.utils;

import java.security.SecureRandom;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;
import java.util.UUID;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class KeyGreaterUtil {

    private static SecureRandom random = new SecureRandom();

    public  String greater(char flag){
        int ran=(int)(Math.random()*900)+100;
        String result=flag+String.valueOf(ran)+getUuid();
        return result;
    }
    public static String[] chars = new String[] { "0", "1", "2", "3", "4", "5",
            "6", "7", "8", "9", "A", "B", "C", "D", "E", "F", "G", "H", "I",
            "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V",
            "W", "X", "Y", "Z" };



    public static String getUuid() {
        StringBuffer shortBuffer = new StringBuffer();
        String uuid = UUID.randomUUID().toString().replace("-", "");
        for (int i = 0; i < 16; i++) {
            String str = uuid.substring(i * 2, i * 2 + 2);
            int x = Integer.parseInt(str, 16);
            shortBuffer.append(chars[x % 36]);
        }
        return shortBuffer.toString();
    }


    /**
     * 获取6-10 的随机位数数字
     * @param length	想要生成的长度
     * @return result
     */
    public static String getRandom620(Integer length) {
        String result = "";
        Random rand = new Random();
        int n = 20;
        if (null != length && length > 0) {
            n = length;
        }
        int randInt = 0;
        for (int i = 0; i < n; i++) {
            randInt = rand.nextInt(10);
            result += randInt;
        }
        return result;
    }

    /**
     * 20 位的订单号
     * @return
     */
    public static String getOrderNo() {
        //格式化当前时间
        SimpleDateFormat sfDate = new SimpleDateFormat("yyMMddHHmmssSSS");
        String strDate = sfDate.format(new Date());
        //为了防止高并发重复,再获取5个随机数
        String random = getRandom620(5);
        //最后得到20位订单编号。
        System.out.println("订单号20位：" + strDate + random);
        return  strDate + random;
    }
    /**
     * 20 位的订单号
     * @return
     */
    public static String getOrderNo(int num) {
        //格式化当前时间
        SimpleDateFormat sfDate = new SimpleDateFormat("yyMMddHHmmssSSS");
        String strDate = sfDate.format(new Date());
        //得到17位时间如：20170411094039080
        //System.out.println("时间17位：" + strDate);
        //为了防止高并发重复,再获取2个随机数
        String random = getRandom620(num);
        //最后得到20位订单编号。
        System.out.println("订单号：" + strDate + random);
        return  strDate + random;
    }

    public static String random_str(int num) {
        //格式化当前时间
        SimpleDateFormat sfDate = new SimpleDateFormat("ssSSS");
        String strDate = sfDate.format(new Date());
        //为了防止高并发重复,再获取3个随机数
        String random = getRandom620(num);
        return  strDate + random;
    }

    /**
     * 使用SecureRandom随机生成Long. (小于= 32 位的随机数)
     */
    public static long randomLong() {
        return Math.abs(random.nextLong());
    }


    public static String randomSTR(int num){
        String str = "";
        for (int i = 0;i<num;i++){
            str = str+ (char)(Math.random()*26+'A');
        }
        return  str;
    }
    public static void main(String[] args){


    }

    /**
     * 验证手机号码
     * @param str
     * @return
     */
    public static boolean isMobile(String str) {
        Pattern p = null;
        Matcher m = null;
        boolean b = false;
        String s2="^((13[0-9])|(14[5,7])|(15[0-3,5-9])|(17[0,3,5-8])|(18[0-9])|166|198|199|(147))\\d{8}$";// 验证手机号
        p = Pattern.compile(s2);
        m = p.matcher(str);
        b = m.matches();
        return b;
    }

}
